package com.mdxx.xczd.utils;

import com.mdxx.xczd.app.ConstantsValue;
import com.mdxx.xczd.bean.Point;
import java.util.List;

public class DistanceUtil {

	private final static double R = 6371;     // 地球半径

	public static double getDistance(double latitude1,double longitude1, double latitude2 ,double longitude2) {
		double lat1 = (Math.PI / 180) * latitude1;
		double lat2 = (Math.PI / 180) * latitude2;
		double lon1 = (Math.PI / 180) * longitude1;
		double lon2 = (Math.PI / 180) * longitude2;
		// 两点间距离 km，如果想要米的话，结果*1000就可以了
		double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;		 
		return d * 1000;
	}

	/**
	 * 起始距离
	 * @param point1
	 * @param point2
	 * @return
	 */
	public static double getDistance(Point point1 ,Point point2) {
		double lat1 = (Math.PI / 180) * point1.la;
		double lat2 = (Math.PI / 180) * point2.la;
		double lon1 = (Math.PI / 180) * point1.lo;
		double lon2 = (Math.PI / 180) * point2.lo;
		double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;		 
		return d * 1000;
	}



	public static Point getmidpoint(double latitude1,double longitude1, double latitude2 ,double longitude2) {
		Point midPoint = new Point();
		midPoint.setLatitude((latitude1 + latitude2)/2);
		midPoint.setLatitude((longitude1 + longitude2)/2);
		return midPoint;
	}

	/**
	 * 中点坐标
	 * @param point1
	 * @param point2
	 * @return 
	 */
	public static Point getmidpoint(Point point1 ,Point point2) {
		Point midPoint = new Point();
		midPoint.setLatitude((point1.la + point2.la)/2);
		midPoint.setLongitude((point1.lo + point2.lo)/2);
		return midPoint;
	}

	private static boolean  otherPoint(List<Point> list, Point midPoint, double distance){
		for (int i = 1; i < list.size()-1; i++) {
			if(distance/2 < getDistance(list.get(i),midPoint)){
				return false;
			}
		}
		return true;
	}


	/**
	 * 是否驻留
	 */
	public static boolean isResident(List<Point> list){
		Point point1 = new Point();
		Point point2 = new Point();
		Point midPoint = null;
		double distance = 0;
		boolean flag = false;
		point1.setLatitude(list.get(0).la);
		point1.setLongitude(list.get(0).lo);
		point2.setLatitude(list.get(list.size()-1).la);
		point2.setLongitude(list.get(list.size()-1).lo);
		distance = getDistance(point1, point2);
		if(distance >  ConstantsValue.distance){
			return false;
		}
		midPoint = getmidpoint(point1, point2);
		flag = otherPoint(list ,midPoint,  ConstantsValue.distance);
		return flag;
	}

}
